#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: messages.py
@contact: zwb15083976291@163.com
@time: 2022/02/27
"""
from common.models import BlogMessage
from common.serializers.serializer import MessageSerializer
from utils.responses import CommonResponse, CommonAPIResponseStatus
from utils.views.views import BaseGenericAPIView


class MessageAdministrationView(BaseGenericAPIView):
    diff_methods_request_params = dict(post=dict(required=['blog_message']))
    serializer_class = MessageSerializer

    def get(self, request):
        # 允许未审核 和 审核通过的数据进行展示
        instance = BlogMessage.objects.filter(audit__in=[0, 1]).all()
        return CommonResponse(
            data=MessageSerializer(instance=instance, serial_fields=['id', 'blog_message'],
                                   many=True).data)

    def post(self, request):
        ret, msg = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST)
        instance = self.serializer.save()
        return CommonResponse(data=MessageSerializer(instance=instance, serial_fields=['id', 'blog_message']).data)
